package com.apestech.framework.xml.condition.modul;

import com.apestech.framework.util.StringUtil;

import java.util.ArrayList;

/**
 * @author hup
 * @create 2019-03-05-15:07
 */
public class TableKey extends StringUtil {
    private String conditionId;
    private String dimensionId;
    private String tabName;
    private String keyValue;
    private String prefix;
    private String suffix;
    private int dimensionSize;
    private int conditionSize;
    private ArrayList<String> tabNameList;
    private ArrayList<String> keyValueList;

    public TableKey(String tabName, String keyValue, String conditionId){
        this.tabName=tabName;
        this.keyValue=keyValue;
        this.conditionId=conditionId;
        conditionSize=conditionId.length();
        tabNameList=getStru(tabName);
        keyValueList=getStru(keyValue);
    }

    public String getTabName(String dimensionId){
        this.dimensionId=dimensionId;
        dimensionSize=dimensionId.length();
        String sid=tabName;
        sid=sid.replace("{conditionId}", conditionId);
        sid=sid.replace("{dimensionId}", dimensionId);
        return sid.toUpperCase();
    }

    public void getKeyValue(String dimensionId){
        this.dimensionId=dimensionId;
        dimensionSize=dimensionId.length();
        prefix=keyValue;
        suffix=null;
        prefix=prefix.replace("{conditionId}", conditionId);
        prefix=prefix.replace("{dimensionId}", dimensionId);
        int pos=prefix.indexOf("{id}");
        int plen=prefix.length();
        if(pos+4<plen-1) suffix=rightStr(prefix, plen-pos-4);
        prefix=leftStr(prefix, pos);
    }

    public String getPrefix() {
        return prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public String getDimensionId() {
        return dimensionId;
    }

    public String getTableFromValue(String value){
        int len=keyValueList.size();
        int st=0;
        int en=0;
        for(int i=0; i<len; i++){
            String k=keyValueList.get(i);
            if(k.indexOf("{")<0) st=st+k.length();
            else{
                if("{conditionId}".equals(k)) st=st+conditionSize;
                else if("{id}".equals(k)) st=st+6;
                else if("{dimensionId}".equals(k)){
                    en=st+dimensionSize;
                    break;
                }
            }
        }
        String dId=value.substring(st, en);
        return  getTabName(dId);
    }

    private ArrayList<String> getStru(String str){
        int len=str.length();
        ArrayList<String> list=new ArrayList<String>();
        for(int i=0; i<len; i++){
            String s=str.substring(i, i+1);
            if("{".equals(s)){
                if(i>0){
                    String s1=null;
                    s1=str.substring(0, i);
                    list.add(s1);
                    str=str.substring(i, len);
                    len=str.length();
                    i=0;
                }
            }
            if("}".equals(s)){
                list.add(str.substring(0, i+1));
                str=str.substring(i+1, len);
                len=str.length();
                i=0;
            }
        }
        return list;
    }

}
